using System.Collections.Generic;
using UnityEngine;

public class PoolManager : MonoBehaviour
{
    public GameObject[] prefabs; // 预制体数组
    List<GameObject>[] pools; // 对象池数组

    void Awake()
    {
        // 初始化对象池数组
        pools = new List<GameObject>[prefabs.Length];
        for (int index = 0; index < pools.Length; index++)
        {
            pools[index] = new List<GameObject>();
        }
    }

    public GameObject Get(int index)
    {
        GameObject select = null;
        // 在对象池中查找未激活的对象
        foreach (GameObject item in pools[index])
        {
            if (!item.activeSelf)
            {
                // 如果找到未激活的对象，选择它并激活
                select = item;
                select.SetActive(true);
                break;
            }
        }
        // 如果对象池中没有未激活的对象，则创建一个新对象
        if (!select)
        {
            select = Instantiate(prefabs[index], transform);
            pools[index].Add(select);
        }
        // 返回选择的对象
        return select;
    }
}